<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">工单说明</label>
        <el-input v-model="query.ordersdesc" clearable placeholder="工单说明" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">工单状态</label>
        <el-input v-model="query.orderstatus" clearable placeholder="工单状态" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">执行结果状态</label>
        <el-input v-model="query.resultstatus" clearable placeholder="执行结果状态" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">指定审核人</label>
        <el-input v-model="query.assignuser" clearable placeholder="指定审核人" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="700px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="90px">
          <el-form-item label="ip" prop="jenkinsid">
            <el-select
              v-model="form.jenkinsid"
              remote
              reserve-keyword
              :loading="loading"
              @change="ipchange"
            >
              <el-option
                v-for="item in options"
                :key="item.jenkinsid"
                :label="item.ip"
                :value="item.jenkinsid"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="操作" prop="operationid">
            <el-select
              v-model="form.tempoperationid"
              remote
              reserve-keyword
              :loading="loading"
              @change="operatioonchange"
            >
              <el-option
                v-for="item in operations"
                :key="item.operationid"
                :label="item.job_desc"
                :value="item.operationid"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="操作参数" prop="operateparams">
            <el-input v-model="form.operateparams" style="width: 370px;" />
          </el-form-item>

          <el-form-item label="工单说明">
            <el-input v-model="form.ordersdesc" :rows="3" type="textarea" style="width: 470px;" />
          </el-form-item>
          <el-form-item label="指定审核人">
            未设置字典，请手动设置 Select
          </el-form-item>
          <el-form-item label="定时执行">
            <el-date-picker v-model="form.exectime" type="datetime" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="ip" label="ip" />
        <el-table-column prop="orderstatus" label="工单状态">
          <template slot-scope="scope">
            {{ dict.label.orderstatus[scope.row.orderstatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="resultstatus" label="执行结果状态">
          <template slot-scope="scope">
            {{ dict.label.resultstatus[scope.row.resultstatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="assignuser" label="指定审核人" />
        <el-table-column prop="exectime" label="定时执行" />
        <el-table-column prop="reguser" label="提交人" />
        <el-table-column prop="regtime" label="提交时间" />
        <el-table-column prop="moduser" label="最后修改人" />
        <el-table-column prop="modtime" label="最后修改时间" />
        <el-table-column v-if="checkPer(['admin','devopsWorkorder:edit','devopsWorkorder:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDevopsWorkorder from '@/api/devops/devopsWorkorder'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { getTrees } from '@/api/devops/devopsJenkinsClient'
// 获取操作

import { getAllOperations } from '@/api/devops/devopsJenkinsOperation'

const defaultForm = { workorderid: null, ordersdesc: null, orderstatus: null, resultstatus: null, assignuser: null, exectime: null, flag: null, notes: null, reguser: null, regtime: null, moduser: null, modtime: null }
export default {
  name: 'DevopsWorkorder',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['orderstatus', 'resultstatus', 'operateneedinput'],
  cruds() {
    return CRUD({ title: 'DEVOPS:工单', url: 'api/devops/devopsWorkorder', idField: 'workorderid', sort: 'workorderid,desc', crudMethod: { ...crudDevopsWorkorder }})
  },
  data() {
    return {
      operateneedinput: [],
      options: [],
      operations: [],
      loading: false,
      permission: {
        add: ['admin', 'devopsWorkorder:add'],
        edit: ['admin', 'devopsWorkorder:edit'],
        del: ['admin', 'devopsWorkorder:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'ordersdesc', display_name: '工单说明' },
        { key: 'orderstatus', display_name: '工单状态' },
        { key: 'resultstatus', display_name: '执行结果状态' },
        { key: 'assignuser', display_name: '指定审核人' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.afterToCU](crud, form) {
      // 得到需要输入参数的数组
      console.log(this.dict.operateneedinput)
      this.dict.operateneedinput.forEach((item, key) => {
        this.operateneedinput.push(item.label)
      })
      console.log(this.operateneedinput)

      this.loading = false
      getTrees().then((res) => {
        this.loading = false
        this.options = res
      }).catch(() => {
        this.loading = false
      })
    },
    getAllOperations(query) {
      this.loading = false
      getAllOperations().then((res) => {
        this.loading = false
        this.operations = res
      }).catch(() => {
        this.loading = false
      })
    },

    ipchange() {
      if (this.form.jenkinsid) {
        this.loading = false
        getAllOperations(this.form.jenkinsid).then((res) => {
          this.loading = false
          this.operations = res.content
        }).catch(() => {
          this.loading = false
        })
      } else {
        this.operateparamsdisabled = true
      }
    },
    operatioonchange() {
      // 切换操作时候，判断 是否需要额外输入参数
      // 获取需要额外输入参数的配置

    }
  }
}
</script>

<style scoped>

</style>
